package graphics.base;

public class Triangle {

	public Triangle setFirstPoint(float x1, float y1, float z1) {
		this.x1 = x1;
		this.y1 = y1;
		this.z1 = z1;
		return this;
	}

	public Triangle setSecodePoint(float x2, float y2, float z2) {
		this.x2 = x2;
		this.y2 = y2;
		this.z2 = z2;
		return this;
	}

	public Triangle setThridPoint(float x3, float y3, float z3) {
		this.x3 = x3;
		this.y3 = y3;
		this.z3 = z3;
		return this;
	}

	/**
	 * before toMoveX or ToMoveY
	 * 
	 * @param rate
	 */
	public void toScale(float rate) {
		toScaleX(rate);
		toScaleY(rate);
		toScaleZ(rate);
	}

	/**
	 * before toMoveX or ToMoveY
	 * 
	 * @param rate
	 */
	public void toScaleX(float rate) {
		this.x1 = this.x1 * rate;
		this.x2 = this.x2 * rate;
		this.x3 = this.x3 * rate;
	}

	/**
	 * before toMoveX or ToMoveY
	 * 
	 * @param rate
	 */
	public void toScaleY(float rate) {
		this.y1 = this.y1 * rate;
		this.y2 = this.y2 * rate;
		this.y3 = this.y3 * rate;
	}

	/**
	 * before toMoveX or ToMoveY
	 * 
	 * @param rate
	 */
	public void toScaleZ(float rate) {
		this.z1 = this.z1 * rate;
		this.z2 = this.z2 * rate;
		this.z3 = this.z3 * rate;
	}

	/**
	 * after toScale
	 * 
	 * @param rate
	 */
	public void toMoveX(float detal) {
		this.x1 += detal;
		this.x2 += detal;
		this.x3 += detal;
	}

	/**
	 * after toScale
	 * 
	 * @param rate
	 */
	public void toMoveY(float detal) {
		this.y1 += detal;
		this.y2 += detal;
		this.y3 += detal;
	}

	/**
	 * after toScale
	 * 
	 * @param rate
	 */
	public void toMoveZ(float detal) {
		this.z1 += detal;
		this.z2 += detal;
		this.z3 += detal;
	}

	/**
	 * 必须所有的点都设置完,才能调用
	 * 
	 * @return
	 */
	public void calculateNormal() {
		// 向量(a,b,c1)和向量(d,e,f1)所在面的法向量(x,y,1)有
		// ax+by+c1=0,dx+ey+f1=0
		float a = x2 - x1;
		float b = y2 - y1;
		float c = z1 - z2;
		// c=-c1
		// ax+by=c
		float d = x3 - x1;
		float e = y3 - y1;
		float f = z1 - z3;
		// f=-f1
		// dx+ey=f
		float x, y;
		if (a != 0) {
			y = (f - d / a * c) / (e - d / a * b);
			x = (c - b * y) / a;
		} else {
			y = c / b;
			x = (f - e * y) / d;
		}
		if (x == -0f)
			x = 0f;
		if (y == -0f)
			y = 0f;
		this.nx = x;
		this.ny = y;
		this.nz = 1.0f;
	}

	public float x1;
	public float y1;
	public float z1;
	//
	public float x2;
	public float y2;
	public float z2;
	//
	public float x3;
	public float y3;
	public float z3;
	// normal
	public float nx;
	public float ny;
	public float nz = 1.0f;

}
